package jmine.tec.di.resolver.impl;


import jmine.tec.annotations.Unmodifiable;
import jmine.tec.di.resolver.ObjectResolver;
import jmine.tec.di.scope.TypedScope;
import jmine.tec.di.wrapper.Wrapper;

/**
 * {@link ObjectResolver} que sempre devolve a mesma instancia
 */
@Unmodifiable
public class StaticObjectResolver implements ObjectResolver {

    private final Object instance;

    /**
     * C'tor
     * 
     * @param instance a instancia a ser resolvida
     */
    public StaticObjectResolver(Object instance) {
        super();
        if (instance == null) {
            throw new NullPointerException();
        }
        this.instance = instance;
    }

    /**
     * devolve a instancia
     * 
     * @param properties properties
     * @param wrapper the wrapper
     * @param requiredComponentClass type
     * @return object
     */
    public Object resolveObject(TypedScope properties, Wrapper wrapper, Class<?> requiredComponentClass) {
        return wrapper.wrap("<auto>", this.instance, this.instance.getClass());
    }

    /**
     * {@inheritDoc}
     * 
     * @see jmine.tec.di.cache.TypeMatch#isTypeCompatible(java.lang.Class)
     */
    public boolean isTypeCompatible(Class<?> requiredType) {
        return requiredType.isInstance(this.instance);
    }

    /**
     * @param properties the properties
     * @return true
     */
    public boolean isResolvable(TypedScope properties) {
        return true;
    }

    /**
     * @return Object
     */
    public Object getInstance() {
        return this.instance;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isStatic() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "StaticResolver: " + this.instance.getClass();
    }

}
